package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.NamedElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @PackageName:com.springboot.reggie.controller
 * @ClassName:AddressBookCtroller
 * @Description: TODO
 * @Author: U87
 * @Date:2022/4/29 16:49
 */
@Slf4j
@RequestMapping("/addressBook")
@RestController
@Api(tags ="地址管理相关接口")
public class AddressBookCtroller {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation(value = "地址查询接口")
    public R<List<AddressBook>> list() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);

    }
    @ApiOperation(value = "添加地址接口")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressBook", value = "地址管理实体", readOnly = true)
    })
    public R<String> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("添加地址成功");
    }

    /**
     * 修改默认地址
     * @param addressBook

     * @return
     */
    @ApiOperation(value = "修改默认地址接口")
    @PutMapping("/default")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressBook", value = "地址管理实体", readOnly = true)
    })
    public R<String> update(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(queryWrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }
    /**
     * 查询默认地址
     */
    @ApiOperation(value = "查询默认地址接口")
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    @ApiOperation(value = "根据id获取地址信息接口")
    @GetMapping("/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址id", readOnly = true)
    })
    public R<AddressBook> getbyId(@PathVariable Long id) {

        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }
    //修改信息
    @ApiOperation(value = "修改信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressBook", value = "地址管理实体", readOnly = true)
    })
    @PutMapping()
    public R<String> updatebyId(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
